package cz.muni.fi.pb138.mathml_canonicalizator;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;
import org.xml.sax.Attributes;

/**
 * This class is a library of utilities that simplify many aspects of work 
 * in this project.
 *
 * @author ndru
 */
public class Util {

    /**
     * Transforms attributes from SAX library to a Map<String, String> which
     * is used in this project.
     * 
     * @param attrs Attributes as given by SAX library.
     * @return Map<String, String> of attributes, where key is attributes name,
     * while its value is attributes value.
     */
    static Map<String, String> attributesToMap(Attributes attrs) {
        Map<String, String> res = new HashMap<String, String>();

        for (int i = 0, len = attrs.getLength(); i < len; i++) {
            res.put(attrs.getQName(i), attrs.getValue(i));
        }

        return res;
    }
   
    /**
     * Returns the list of white-space separated properties of a specified key
     * from a specified property file.
     * 
     * @param property The property whose value is to be returned.
     * @param source The property file.
     * @return List of strings that are values of given property from a
     * specified file separated by a white-space.
     */
    static List<String> getProperty(String property, String source) {
        Properties config = new Properties();
        String result[] = null;
        ClassLoader classLoader = ClassLoader.getSystemClassLoader();  
       
        try {
            config.load(classLoader.getClass().getResourceAsStream("/" + source));
            if (config.getProperty(property) != null) {
                // there is no such property
                result = config.getProperty(property).split(" ");
            }
        } catch (IOException e) {
            System.err.println("Could not open configuration file for " + source);
            System.err.println(e.getMessage());
        }
       
        return result == null ? new ArrayList<String>() : Arrays.asList(result);
    }

    /**
     * Removes attributes from a map.
     * 
     * @param attrs Map of attributes.
     * @param attr_keep List of attributes which defines the attributes that
     * will not be removed.
     */
    static void removeAtrributes(Map<String, String> attrs, List<String> attr_keep) {
        // removes all attributes that should be removed
        Collection<String> c = attrs.keySet();
        Iterator it = c.iterator();
        while (it.hasNext()) {
            if (!attr_keep.contains((String) it.next())) {
                it.remove();
            }
        }
    }
   
    /**
     * Returns a map of all values of all properties from a specified .properties
     * file.
     * 
     * @param filename The file that properties should be read from.
     * @param propertyFile Properties object.
     * @return Map<String, List<String>> where maps keys are names of properties
     * and their values are split by spaces and saved into lists of strings.
     */
    static Map<String, List<String>> getAllProperties(String filename,
            Properties propertyFile) {
        Map<String, List<String>> properties = new HashMap<String, List<String>>();
        ClassLoader classLoader = ClassLoader.getSystemClassLoader();  
       
        try {
            propertyFile.load(classLoader.getClass().getResourceAsStream("/" + filename));
            Set<Object> AttrSet = propertyFile.keySet();
            for (Object o : AttrSet) {
                String key = o instanceof String ? (String) o : new String();
                if (key != null) {
                    // key should always be not null
                    properties.put(key, Arrays.asList(propertyFile.getProperty(key).split(" ")));
                }
            }
        } catch (IOException e) {
            System.err.println("Could not open configuration file: " + filename);
            System.err.println(e.getMessage());
        }
       
        return properties;
    }
}

